package jungletimers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author nmaludy
 */
public class JungleTimersFrame extends JFrame implements ActionListener {
    
    private static final long serialVersionUID = 1L;

    private final Timer timer;
    private final TeamPanel allyTeamPanel;
    private final TeamPanel enemyTeamPanel;
    private final NeutralPanel neutralPanel;
    private final JButton startStopButton;

    public JungleTimersFrame() {
        super("Jungle Timers");
        this.timer = new Timer(1000, null);
        this.timer.setInitialDelay(0);
        this.allyTeamPanel = TeamPanelFactory.newAllyTeamPanel(timer);
        this.enemyTeamPanel = TeamPanelFactory.newEnemyTeamPanel(timer);
        this.neutralPanel = new NeutralPanel(timer);
        this.startStopButton = new JButton("Start");
        this.startStopButton.addActionListener(this);
        layoutUi();
        super.pack();
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setLocationRelativeTo(null);
    }

    private void layoutUi() {
        MigLayout ml = new MigLayout("insets 10 10 10 10", //Layout constraints
                "[fill][fill]", //column constraints
                "[fill]"); //row constraints
        super.setLayout(ml);
        super.add(allyTeamPanel, "grow");
        super.add(enemyTeamPanel, "grow, wrap");
        super.add(neutralPanel, "grow, span 2, wrap");
        super.add(startStopButton, "grow, span 2");
    }

    public void actionPerformed(ActionEvent e) {
        if (startStopButton.equals(e.getSource())) {
            startStop();
        }
    }

    private void startStop() {
        if (timer.isRunning()) {
            startStopButton.setText("Start");
            timer.stop();
            allyTeamPanel.resetTimers();
            enemyTeamPanel.resetTimers();
            neutralPanel.resetTimers();
        } else {
            startStopButton.setText("Stop and Reset");
            timer.start();
        }
    }

}
